1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.fhw.cabaweb.webfrontend.filters;
20
21 import java.io.IOException;
22 import java.io.OutputStreamWriter;
23 import java.io.PrintWriter;
24
25 import javax.servlet.ServletOutputStream;
26 import javax.servlet.http.HttpServletResponse;
27 import javax.servlet.http.HttpServletResponseWrapper;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 /***
33 * <strong>Filter</strong>-Klasse die im Falle das der Client das unterstützt,
34 * den Stream GZIP komprimiert. Antwort Wrapper Klasse.
35 *
36 * ORIGINAL aus dem Buch Servlets and JavaServer Pages
37 * von Jayson Falkner, Kevin Jones (http://www.jspbook.com/)
38 *
39 * @author Jayson Falkner, Kevin Jones
40 *
41 * @version Version 1.0 24.07.2004
42 */
43 public class GZIPResponseWrapper extends HttpServletResponseWrapper
44 {
45 /***
46 * The <code>Log</code> instance for this application.
47 */
48 private Log log = LogFactory.getLog("org.fhw.cabaweb.webfrontend.filters");
49
50 protected HttpServletResponse origResponse = null;
51 protected ServletOutputStream stream = null;
52 protected PrintWriter writer = null;
53
54 public GZIPResponseWrapper(HttpServletResponse response)
55 {
56 super(response);
57 origResponse = response;
58 }
59
60 public ServletOutputStream createOutputStream() throws IOException
61 {
62 return (new GZIPResponseStream(origResponse));
63 }
64
65 public void finishResponse()
66 {
67 try
68 {
69 if (writer != null)
70 {
71 writer.close();
72 }
73 else
74 {
75 if (stream != null)
76 {
77 stream.close();
78 }
79 }
80 }
81 catch (IOException e)
82 {
83 log.error(" GZIP IOException!");
84 log.error(e.getStackTrace());
85 }
86 }
87
88 public void flushBuffer() throws IOException
89 {
90 stream.flush();
91 }
92
93 public ServletOutputStream getOutputStream() throws IOException
94 {
95 if (writer != null)
96 {
97 log.error(" GZIP getWriter() has already been called!");
98 throw new IllegalStateException("getWriter() has already been called!");
99 }
100
101 if (stream == null)
102 stream = createOutputStream();
103
104 return (stream);
105 }
106
107 public PrintWriter getWriter() throws IOException
108 {
109 if (writer != null)
110 {
111 return (writer);
112 }
113
114 if (stream != null)
115 {
116 log.error(" GZIP getOutputStream() has already been called!");
117 throw new IllegalStateException("getOutputStream() has already been called!");
118 }
119
120 stream = createOutputStream();
121
122 writer = new PrintWriter(new OutputStreamWriter(stream, origResponse.getCharacterEncoding()));
123
124 return (writer);
125 }
126
127 public void setContentLength(int length)
128 {
129 }
130 }